<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 14:20
 */

namespace app\controller\index\plan;


use app\controller\index\BaseController;
use app\exception\IndexException;
use app\logic\index\plan\PlanLogic;
use support\Request;
use support\Response;

class IndexController extends BaseController
{
    /**
     * 扣除点数，立即生成
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function generateAndConsume(Request $request):Response
    {
        $param = [];
        $param['novel_sn'] = $request->input('novel_sn');
        $param['planid'] = $request->input('planid');
        $param['articleid'] = $request->input('articleid');
        $param['videoid'] = $request->input('videoid');
        $param['overseaid'] = $request->input('overseaid');
        $param['parts'] = $request->input('parts');
        if(empty($param['novel_sn']) && empty($param['planid']) && empty($param['articleid']) && empty($param['videoid'])
            && empty($param['overseaid'])){
            throw new IndexException(4020,'推文不存在');
        }

        $data = PlanLogic::getInstance()->generateAndConsume($param);

        return $this->endSuccess($data);
    }

    public function getUserPartAiImage(Request $request):Response
    {
        $param = [];
        $param['requestid'] = $request->input('requestid');
        $param['user_partid'] = $request->input('user_partid');
        $param['is_again'] = $request->input('is_again');
        if(empty($param['user_partid'])){
            throw new IndexException(4020,'数据不存在');
        }
        if(empty($param['requestid'])){
            throw new IndexException(4020,'参数错误');
        }

        $data = PlanLogic::getInstance()->getUserPartAiImage($param);

        return $this->endSuccess($data);
    }

    public function updateUserPlan(Request $request):Response
    {
        $param = [];
        $param['user_partid'] = $request->input('user_partid');
        $param['analyze_content'] = $request->input('analyze_content');
        $param['prompt'] = $request->input('prompt');
        $param['ai_image'] = $request->input('ai_image');
//        $param['fontid'] = $request->input('fontid');
//        $param['video_rate'] = $request->input('video_rate');
        if(empty($param['user_partid'])){
            throw new IndexException(4020,'数据不存在');
        }

        $data = PlanLogic::getInstance()->updateUserPlan($param);

        return $this->endSuccess($data);
    }

    /**
     * 重新生成图片
     * @param Request $request
     * @return Response
     * @throws IndexException
     */
    public function generatedAiImage(Request $request):Response
    {
        $param = [];
        $param['user_partid'] = $request->input('user_partid');
        $param['issave'] = $request->input('issave',1);
        $param['is_fix'] = $request->input('is_fix',0); //如果为1的时候取调用新的指令
        if(empty($param['user_partid'])){
            throw new IndexException(4020,'数据不存在');
        }

        $data = PlanLogic::getInstance()->generatedAiImage($param);

        return $this->endSuccess($data);
    }

    public function generatedVideo(Request $request):Response
    {
        $param = [];
        $param['user_planid'] = $request->input('user_planid');
        $param['timbreid'] = $request->input('timbreid',null);
        $param['timbre_volume'] = $request->input('timbre_volume',null);
        $param['speak_speed'] = $request->input('speak_speed',null);
        $param['musicid'] = $request->input('musicid',null);
        $param['music_volume'] = $request->input('music_volume',null);
        $param['fontid'] = $request->input('fontid',null);
        $param['video_rate'] = $request->input('video_rate',null);

        if(empty($param['user_planid'])){
            throw new IndexException(4020,'数据不存在');
        }

        $data = PlanLogic::getInstance()->generatedVideo($param);

        return $this->endSuccess($data);
    }

}